package com.xhh.nphm.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xhh.nphm.system.domain.TwfDeferEty;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public interface TwfDeferDao extends BaseMapper<TwfDeferEty> {
    /**
     * 延期申请列表数据
     *
     * @param sendId 拟派工单id
     * @return
     */
    @Select("select PkId as myPkVal,WorkFormNO,SDeptName,convert(varchar(19),LimitOld,20) as tmLimit," +
            "convert(varchar(19),DelayTo,20) as tmDelay,DelayRN,DeferState as chgState,ChkDesc,PkId as opDeal from tWfDefer " +
            "where SendID=#{sendId} and IsDel=0 [[and SDeptName =#{sdeptName}]] [[and DelayRN =#{delayRN}]] order by PkId")
    List<Map<String,Object>> twfDeferListJson(@Param("sendId")String sendId,@Param("sdeptName")String sdeptName,@Param("delayRN")String delayRN);

    /**
     * 根据 wfid 查询 延期申请数据
     *
     * @param wfId
     * @return
     */
    @Select("select * from tWfDefer where WfId= #{wfId} ")
    TwfDeferEty selectByWfId(@Param("wfId") Integer wfId);

    /**
     * 延期申请-已审列表数据
     *
     * @return
     */
    @Select("select PkId,SendID,WfId,WorkFormNO,ADeptName,convert(varchar(10),LimitOld,20) as limitTime,convert(varchar(19),DelayTo,20) as delay,dbo.deferCount(WfId,ADeptNo) as deferCount,DelayRN,DeferState from tWfDefer " +
            "where SDeptID=#{sdeptId} and DeferState in (21,22) and IsDel=0 [[and WorkFormNO = #{twfDefer.workFormNo}]] [[and ADeptName =#{twfDefer.adeptName}]]")
    List<TwfDeferEty> tDeferChkNJson(@Param("twfDefer") TwfDeferEty twfDeferEty,@Param("sdeptId") String sdeptId );

    /**
     * 延期申请-未审列表数据
     *
     * @return
     */
    @Select("select PkId,SendID,WfId,WorkFormNO,ADeptName,convert(varchar(10),LimitOld,20) as limitTime,convert(varchar(19),DelayTo,20) as delay,dbo.deferCount(WfId,ADeptNo) as deferCount,DelayRN,DeferState from tWfDefer " +
            "where SDeptID=#{sdeptId} and DeferState = 10 and IsDel=0 [[and WorkFormNO = #{twfDefer.workFormNo}]] [[and ADeptName =#{twfDefer.adeptName}]]")
    List<TwfDeferEty> tDeferUntriedJson(@Param("twfDefer") TwfDeferEty twfDeferEty,@Param("sdeptId") String sdeptId );


    @Select("select PkId,SendID,WfId,WorkFormNO,ADeptName,convert(varchar(10),LimitOld,20) as limitTime,convert(varchar(19),DelayTo,20) as delay,dbo.deferCount(WfId,ADeptNo) as deferCount,DelayRN,DeferState from tWfDefer " +
            "where SDeptID=#{sdeptId} and DeferState = 10 and IsDel=0 AND DATEDIFF(day, GETDATE(), LimitOld) > -7 [[and WorkFormNO = #{twfDefer.workFormNo}]] [[and ADeptName =#{twfDefer.adeptName}]]")
    List<TwfDeferEty> tDeferUntriedJson_gxq(@Param("twfDefer") TwfDeferEty twfDeferEty,@Param("sdeptId") String sdeptId );

    /**
     * 根据工单id 查询延期列表
     * @param wfId
     * @return
     */
    @Select("select PkId as myPkVal,WfId,SDeptName,SUserName,convert(varchar(19),LimitOld,20) as oldTime,ADeptName,AUserName,convert(varchar(19),DelayTo,20) as delayTo,DeferState,ChkUserName,ChkDesc,PkId as myOP from tWfDefer where WfId=#{wfId} and IsDel=0 order by PkId")
    List<Map<String,Object>> deferListJson(Integer wfId);

    /**
     * 查询本部门的电话延期申请列表
     *
     * @param params 查询条件
     * @return 列表数据
     * @author zz
     */
    @Select("select PkId,SendID,WfId,WorkFormNO,ADeptName,convert(varchar(10),LimitOld,20) as limitTime," +
            "convert(varchar(19),DelayTo,20) as delay,DelayRN,DeferState,convert(varchar(10),CheckTime,20) as chkTime from tWfDefer " +
            "where ADeptNo=#{deptNo} and IsDel=0 [[and WorkFormNO = #{twfDefer.workFormNo}]] [[and ADeptName =#{twfDefer.adeptName}]]")
    List<TwfDeferEty> tDeferResultJson(HashMap<String, Object> params);
}
